import axios from "axios";
import router from "../router";
let baseUrl = "http://localhost:8080"
// let token = localStorage.getItem("token")
//main.js
import Message from 'element-ui/packages/message/src/main'
// import router from
//创建axios的实例
const service = axios.create({
//接口前缀
    baseURL:baseUrl,
//设置接口的超时时间
    timeout:5000,
})
//请求的拦截器
service.interceptors.request.use((config)=>{
    //将登录凭证添加到请求头   (动态获取)
    config.headers['token'] = localStorage.getItem("token");
    console.log(config.method);
    console.log(config.url);
    return config;
})
//请求响应拦截器
service.interceptors.response.use((res)=>{
    console.log("响应拦截器:"+res)
    if (res.data.code===500){
        Message.error(res.data.msg)
        return false;
    }else if (res.data.code===401){
        Message.error(res.data.msg)
        router.push("/login")
        return false;
    }
    return res.data;
})
export function sendRequest(method,url,data){
    return service({
        method:method,
        url:url,
        data:data
    })
}
export function getBaseUrl(){
    return baseUrl;
}
